<?php

require_once 'web.php';

/**
 * Description of HomeController
 *
 * @author user
 */
class HomeController extends WebController {

    //put your code here

    public function index() {
        URL::redirect('home/random');
    }

    public function _before() {
        parent::_before();
        $this->view->content = View::factory('home/index');
    }

    public function newest($category_id = 0, $page = 1) {
        $this->set_thumbnails($category_id, $page, "home/newest/", "newest");
    }

    public function top($category_id = 0, $page = 1) {
        $this->set_thumbnails($category_id, $page, "home/top/", "top");
    }

    public function favourite($category_id = 0, $page = 1) {
        $this->set_thumbnails($category_id, $page, "home/favourite/", "favourite");
    }

    public function random($category_id = 0, $page = 1) {
        $this->set_thumbnails($category_id, $page, "home/random/", "random");
    }

    private function set_thumbnails($category_id, $page, $pagination_url, $type = null) {
        $view_user = $this->view_user;
        $auth_user = $this->auth_user->user;

        // CATEGORY NAME
        $category = "All";
        if ($category_id != 0) {
            $category = new ORM('photo_category');
            $category->find($category_id);
            $category = $category->name;
        }

        // CATEGORY LINK
        $get = "";

        $categ = new ORM('photo_category');
        $categ = $categ->find_all();

        $category_link = array();
        $category_link["0$get"]['value'] = "All";

        foreach ($categ as $c) {
            if ($c->alias_name != 'no_category') {
                $category_link[$c->id . $get]['value'] = $c->name;
            }
        }

        // Set visibility for category link
        $category_link[$category_id . $get]['active'] = true;

        // MAKE QUERY
        $user = $view_user;
        $query = "
            SELECT *
            FROM `photo`
            JOIN `user` ON `user`.`id` = `photo`.`owner`
            JOIN `photo_category` ON `photo_category`.`id` = `photo`.`category_id`
            ";

        if ($type == "top") {
            $query = "
            SELECT *, IFNULL(SUM(`mark`), 0) AS `mark`
            FROM `photo`
            JOIN `user` ON `user`.`id` = `photo`.`owner`
            JOIN `photo_category` ON `photo_category`.`id` = `photo`.`category_id`
            LEFT JOIN  `photo_mark` ON  `photo`.`id` =  `photo_mark`.`photo_id` 
            ";
        } else if ($type == "favourite") {
            $query = "
                SELECT *, IFNULL(COUNT(`photo_comment`.`photo_id`), 0) AS `count`
                FROM `photo`
                JOIN `user` ON `user`.`id` = `photo`.`owner`
                JOIN `photo_category` ON `photo_category`.`id` = `photo`.`category_id`
                LEFT JOIN `photo_comment` ON `photo_comment`.`photo_id` = `photo`.`id`
                ";
        }

        // View Access
        $where = "";
        if (isset($this->auth_user->user)) {
            $where .= " WHERE `view_access` <= 2";
        } else {
            $where = " WHERE  `view_access` = '1'";
        }

        // Category
        if ($category_id != 0) {
            $where .= " AND `category_id` = '$category_id'";
        }
        $query .= $where;

        // Group by and order by
        if ($type == "top") {
            $query .= " GROUP BY `photo`.`id` ORDER BY `mark` DESC, `photo`.`id` ASC";
        } else if ($type == "favourite") {
            $query .= " GROUP BY `photo`.`id` ORDER BY `count` DESC, `photo`.`id` ASC";
        } else {
            $query .= " ORDER BY `photo`.`id` DESC";
        }

        // EXECUTE QUERY
        $photos = DBQuery::instance()->query($query);

        // Shuffle photos
        if ($type == "random") {
            shuffle($photos);
        }

        // PHOTO PAGINATION
        $number_per_page = 6;
        $num_photos = count($photos);
        $offset = ($page - 1) * $number_per_page;
        $max_page = ceil($num_photos / $number_per_page);

        if ($type == "random") {
            $offset = 0;
            $max_page = 1;
        }

        $photos = array_slice($photos, $offset, $number_per_page);


        // SET VIEW
        $data = array(
            'photos' => $photos,
            'page' => $page,
            'max_page' => $max_page,
            'category_id' => $category_id,
            'pagination_url' => $pagination_url,
            'category_link' => $category_link,
            'get' => $get,
            'auth_user' => $this->auth_user->user,
            'tagged_category' => false
        );
        $this->view->content = View::factory('home/index');
        $this->view->content->thumbnail = View::factory('photo/thumbnail', $data);

        // AJAX REQUEST
        if (isset($_POST['ajax'])) {
            $this->view = new View('ajax', array('content' => $this->view->content->thumbnail));
            return;
        }
    }

    public function _after() {
        $home_links = array(
            "home/newest" => array('value' => 'Newest Photos', 'active' => false),
            "home/favourite" => array('value' => 'Favourite Photos', 'active' => false),
            "home/top" => array('value' => 'Top Photos', 'active' => false),
            "home/random" => array('value' => 'Random Photos', 'active' => false),
        );

        $home_links[$this->router->controller . "/" . $this->router->method]['active'] = true;

        $this->view->content->home_links = $home_links;

        parent::_after();
    }

}

?>
